﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace DataContext
{
	public class ServiceContext
	{
		#region Fields
		Database.Database db = new Database.Database(Database.Providers.SqlClient, Config.Global.Settings.ConnectionString);
		#endregion

		#region Constructors
		public ServiceContext() {}
		#endregion

		#region Context Methods
        /// <summary>
        /// Kiểm tra đã tồn tại Link dịch vụ hay chưa ?
        /// </summary>
        /// <param name="serviceURL"></param>
        /// <returns>#0: Tồn tại</returns>
        public int CheckExistsServiceURL(string serviceURL)
        {
            try
            {
                List<SqlParameter> lstParam = new List<SqlParameter>();
                lstParam.Add(new SqlParameter("@ServiceURL", serviceURL));
                return db.ExecuteStoreProcedure("[uspServiceCheckExistsServiceURL]", lstParam.ToArray());
            }
            catch (System.Exception)
            {
                return 1;
            }
        }

        /// <summary>
        /// Cập nhật Trạng thái Dịch vụ
        /// </summary>
        /// <param name="serviceID"></param>
        /// <returns></returns>
        public bool UpdateStatus(int serviceID)
        {
            try
            {
                List<SqlParameter> lstParam = new List<SqlParameter>();
                lstParam.Add(new SqlParameter("@ServiceID", serviceID));
                return db.ExecuteStoreProcedure("[uspServiceUpdateStatus]", lstParam.ToArray()) > 0;
            }
            catch (System.Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Lấy ServiceID theo ServiceURL
        /// </summary>
        /// <param name="serviceURL"></param>
        /// <returns></returns>
        public int SelectServiceID(string serviceURL)
        {
            try
            {
                List<SqlParameter> lstParam = new List<SqlParameter>();
                lstParam.Add(new SqlParameter("@ServiceURL", serviceURL));

                return db.ExecuteStoreProcedure("[uspServiceSelectServiceIDByServiceURL]", lstParam.ToArray());
            }
            catch (System.Exception)
            {
                return 0;
            }
        }
		#endregion
	}
}
